using System;
using DG.Tweening;
using GameFramework;
using GameFramework.Resource;
using StarForce.Manager;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace StarForce
{
    /// <summary>
    /// 三消单个方块Item脚本
    /// </summary>
    public partial class EliminateItem : CustomMono, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler
    {
        /// <summary>
        /// 背景图片
        /// </summary>
        [SerializeField] private Image m_Bg;


        //todo 测试方便看
        [SerializeField] private Text m_Pos;

        /// <summary>
        /// 上方Item
        /// </summary>
        private EliminateItem m_TItem;

        /// <summary>
        /// 下方Item
        /// </summary>
        private EliminateItem m_BItem;

        /// <summary>
        /// 左边Item
        /// </summary>
        private EliminateItem m_LItem;

        /// <summary>
        /// 右边Item
        /// </summary>
        private EliminateItem m_RItem;

        private EliminateItemData m_EliminateItemData;

        /// <summary>
        /// Item 宽度
        /// </summary>
        private float m_Width;

        /// <summary>
        /// Item 高度
        /// </summary>
        private float m_Height;

        private RectTransform m_RectTransform;

        private LoadAssetCallbacks m_LoadAssetCallbacks;

        /// <summary>
        /// 起始点
        /// </summary>
        private Vector3 m_StartPointer;

        /// <summary>
        /// 抬起点
        /// </summary>
        private Vector3 m_EndPointer;

        public EliminateItem TItem
        {
            get { return GameEntry.Eliminate.GetTopItem(Pos); }
            private set { m_TItem = value; }
        }

        public EliminateItem BItem
        {
            get { return GameEntry.Eliminate.GetBottomItem(Pos); }
            private set { m_BItem = value; }
        }

        public EliminateItem LItem
        {
            get { return GameEntry.Eliminate.GetLeftItem(Pos); }
            private set { m_LItem = value; }
        }

        public EliminateItem RItem
        {
            get { return GameEntry.Eliminate.GetRightItem(Pos); }
            private set { m_RItem = value; }
        }


        public int Pos
        {
            get { return m_EliminateItemData.Post; }

            private set { Pos = value; }
        }

        public EliminateType Type
        {
            get { return m_EliminateItemData.EliminateType; }
        }


        private void Awake()
        {
            m_RectTransform = transform.GetComponent<RectTransform>();
            m_Width = m_RectTransform.sizeDelta.x;
            m_Height = m_RectTransform.sizeDelta.y;
            m_LoadAssetCallbacks = new LoadAssetCallbacks(LoadAssetsSuccess, LoadAssetsFailure);
        }

        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="eliminateItemData"></param>
        public void Init(EliminateItemData eliminateItemData)
        {
            gameObject.SetActive(true);
            transform.localScale = Vector3.one;
            m_EliminateItemData = eliminateItemData;

            if (!m_EliminateItemData.IsMove)
            {
                //todo 初始生成的方块 直接生成到位置
                transform.localPosition = GetPosition(m_EliminateItemData.Post);
            }
            else
            {
                //todo 后期填充的方块移动到指定位置
                Vector3 pos = GetPosition(m_EliminateItemData.Post);
                pos.y = 470f + m_Height;

                transform.localPosition = pos;
                MoveTo(m_EliminateItemData.Post);
            }

//            SetPosition(m_EliminateItemData.Post);
            //加载资源图片 或者其他效果
            m_Pos.text = m_EliminateItemData.Post.ToString();
            string imgPath = AssetUtility.GetEliminateImg(m_EliminateItemData.BgPath);
            GameEntry.Resource.LoadAsset(imgPath, typeof(Sprite),
                Constant.AssetPriority.ImageAsset, m_LoadAssetCallbacks, m_Bg);
        }

        /// <summary>
        /// 重置方法
        /// </summary>
        public void Reset()
        {
            gameObject.SetActive(false);
            m_TItem = null;
            m_BItem = null;
            m_LItem = null;
            m_RItem = null;
        }

        /// <summary>
        /// 设置方块初始位置
        /// </summary>
        private Vector3 GetPosition(int pos)
        {
            //1.根据post 计算位置
            float startX = -330f;

            float startY = 460f;

            //2.偏移值

            float offsetX = 10f;

            float offsetY = 10f;

            int HIndex = 0; //列

            int VIndex = 0; //行


            //判断是第几列

            HIndex = (int) Math.Floor((float) (pos / Constant.EliminateGameConfig.HCount));

            //判断是第几行
            VIndex = pos % Constant.EliminateGameConfig.VCount;

            //transform.localPosition = new Vector3(startX + HIndex*(m_Width+ offsetX ),startY  - VIndex*(m_Height+ offsetY) ,0);
            return new Vector3(startX + HIndex * (m_Width + offsetX), startY - VIndex * (m_Height + offsetY), 0);
        }

        /// <summary>
        /// 移动方块
        /// </summary>
        public void MoveTo(int pos)
        {
            transform.DOLocalMove(GetPosition(pos), 0.5f);
            m_EliminateItemData.Post = pos;
            m_Pos.text = Pos.ToString();
        }

        public void Exchange(int pos)
        {
            m_EliminateItemData.Post = pos;
            m_Pos.text = Pos.ToString();
        }


        /// <summary>
        /// 加载资源成功回调
        /// </summary>
        /// <param name="assetname"></param>
        /// <param name="asset"></param>
        /// <param name="duration"></param>
        /// <param name="userdata"></param>
        private void LoadAssetsSuccess(string assetname, object asset, float duration, object userdata)
        {
            if (asset is Sprite && userdata is Image)
            {
                //加载的是图片
                Sprite sprite = asset as Sprite;
                Image img = (Image) userdata;

                img.sprite = sprite;
            }
        }

        /// <summary>
        /// 加载资源失败回调
        /// </summary>
        /// <param name="assetname"></param>
        /// <param name="progress"></param>
        /// <param name="userdata"></param>
        private void LoadAssetsFailure(string assetname, float progress, object userdata)
        {
            Log.Warning("LoadAssetsFailure");
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
//            Log.Info("鼠标移入事件");
        }

        public void OnPointerDown(PointerEventData eventData)
        {
//            Log.Info("鼠标按下");
//            
//            Log.Info("Top:{0}" , TItem == null ? -1:TItem.Pos);
//            Log.Info("Bottom:{0}" , BItem == null ? -1:BItem.Pos);
//            Log.Info("Left:{0}" , LItem == null ? -1:LItem.Pos);
//            Log.Info("Right:{0}" , RItem == null ? -1:RItem.Pos);

            m_StartPointer = Input.mousePosition;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            m_EndPointer = Input.mousePosition;
            float offsetX = m_EndPointer.x - m_StartPointer.x;
            float offsetY = m_EndPointer.y - m_StartPointer.y;

            if (Math.Abs(offsetX) > m_Width)
            {
                if (offsetX > 0)
                {
                    GameEntry.Eliminate.ExChange(this, RItem);
                    Log.Info("右移动");
                }
                else
                {
                    GameEntry.Eliminate.ExChange(this, LItem);
                    Log.Info("左移动");
                }
            }
            else if (Math.Abs(offsetY) > m_Height)

            {
                if (offsetY > 0)
                {
                    GameEntry.Eliminate.ExChange(this, TItem);
                    Log.Info("上移动");
                }
                else
                {
                    GameEntry.Eliminate.ExChange(this, BItem);
                    Log.Info("下移动");
                }
            }
        }
    }
}